package com.genesis.yunnanji.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.DiZhouJingDianDetailsBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;

import org.json.JSONObject;
import org.michaelbel.bottomsheet.BottomSheet;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;

import java.net.URISyntaxException;

import es.dmoral.toasty.Toasty;

/**
 * 创建日期：2018/11/2 0002
 * 作者：Kite
 * 描述：景点详情
 */
public class DiZhouJingDianDetailsActivity extends BaseActivity {

    @ViewInject(R.id.toolbarLayout)
    CollapsingToolbarLayout toolbarLayout;

    @ViewInject(R.id.toolbar)
    Toolbar toolbar;

    @ViewInject(R.id.tvIntroduce)
    TextView tvIntroduce;

    @ViewInject(R.id.tvDetailsTime)
    TextView tvDetailsTime;

    @ViewInject(R.id.tvDetailsPhone)
    TextView tvDetailsPhone;

    @ViewInject(R.id.tvDetailsAddress)
    TextView tvDetailsAddress;

    @ViewInject(R.id.tvDetailsNavigation)
    TextView tvDetailsNavigation;

    @ViewInject(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;

    @ViewInject(R.id.mapDetails)
    MapView mMapView;

    private AMap mAmap;

    private String address;

    private double lat;
    private double lon;

    private int[] items1 = new int[]{
            R.string.baidu,
            R.string.gaode,
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_di_zhou_jing_dian_details);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        toolbar.setNavigationIcon(R.mipmap.return_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mMapView.onCreate(savedInstanceState);
        if (mAmap == null) {
            mAmap = mMapView.getMap();
        }
    }

    @Override
    protected void setUpView() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        initDate(id);
    }

    private void initDate(String id) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.DIZHOU_JINGIDAN_INFO);
        params.addBodyParameter("id", id);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                DiZhouJingDianDetailsBean bean = gson.fromJson(jsonObject.toString(), DiZhouJingDianDetailsBean.class);
                if (bean.getStatus() == 200) {
                    toolbar.setTitle(bean.getResult().getInfo().getTitle());
                    tvIntroduce.setText(bean.getResult().getInfo().getDescription());
                    tvDetailsTime.setText(bean.getResult().getInfo().getTicket_price());
                    tvDetailsPhone.setText(bean.getResult().getInfo().getAddr());
                    tvDetailsAddress.setText(bean.getResult().getInfo().getOpendate());
                    address = bean.getResult().getInfo().getAddr();
                    SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                            toolbarLayout.setBackground(resource);
                        }
                    };
                    Glide.with(DiZhouJingDianDetailsActivity.this).load(GenesisApiConfig.PIC_HOST + bean.getResult().getInfo().getPicture().get(0)).into(simpleTarget);
                    lat = Double.parseDouble(bean.getResult().getInfo().getLng());
                    lon = Double.parseDouble(bean.getResult().getInfo().getLat());
                    setupmap(lat, lon);
                    tvDetailsNavigation.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BottomSheet.Builder builder = new BottomSheet.Builder(context);
                            builder.setItems(items1, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which) {
                                        case 0:
                                            if (genesisUtils.isAvilible(context, "com.baidu.BaiduMap")) {
                                                try {
                                                    Intent intent = Intent.getIntent("intent://map/direction?origin=我的位置&destination=" + address + "&mode=driving&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                                                    context.startActivity(intent);
                                                } catch (URISyntaxException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                Toasty.error(context, "您尚未安装百度地图").show();
                                            }
                                            break;
                                        case 1:
                                            if (genesisUtils.isAvilible(context, "com.autonavi.minimap")) {
                                                try {
                                                    Intent intent = Intent.getIntent("androidamap://route?sourceApplication=softname&sname=我的位置&dlat=" + lat + "&dlon=" + lon + "&dname=" + address + "&dev=0&m=0&t=1");
                                                    context.startActivity(intent);
                                                } catch (URISyntaxException e) {
                                                    e.printStackTrace();
                                                }
                                            } else {
                                                Toasty.error(context, "您尚未安装高德地图").show();
                                            }
                                            break;
                                    }
                                }
                            });
                            builder.setDividers(true);
                            builder.show();
                        }
                    });
                } else {
                    Toast.makeText(DiZhouJingDianDetailsActivity.this, bean.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onError(String errorStr) {
            }
        });
    }

    public void setupmap(final Double lat, final Double lng) {
        LatLng latLng = new LatLng(lat, lng);
        mAmap.addMarker(new MarkerOptions().position(latLng).draggable(false));
        mAmap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
        mAmap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
            @Override
            public void onTouch(MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                switch (action) {
                    case MotionEvent.ACTION_UP:
                        nestedScrollView.requestDisallowInterceptTouchEvent(false);
                        break;
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        nestedScrollView.requestDisallowInterceptTouchEvent(true);
                        break;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

}
